﻿using System.Data;
using System.Linq;
using System.Web.Mvc;
using CVsharp.Models.Entities;

namespace CVsharp.DWZMvc.Controllers
{
    public class SystemManageController : Controller
    {
        //
        // GET: /SystemManage/

        public ActionResult Index()
        {
            return View();
        }

        //
        // GET:/SystemManage/UserList

        public ActionResult UserList()
        {
            return PartialView();
        }

        //
        // GET:/SystemManage/UserOnline

        public ActionResult UserOnline()
        {
            var dt = (DataTable) System.Web.HttpContext.Current.Application["OnlineTalbe"];
            //dt.DefaultView.RowFilter = "VisitorName<>'游客'";
            //dt.DefaultView.Sort = "LastActiveTime";
            ViewBag.useronliedata = dt;
            return PartialView();
        }

        [HttpGet]
        public ActionResult UserAcctionLog()
        {
            var db = new DBEntities();
            ViewBag.pageNum = 1;
            ViewBag.numPerPage = 20;
            ViewBag.keywords = "";
            ViewBag.totalnum = db.CommonLog.Count();
            return View(db.CommonLog.Take(20).ToList());
        }

        [HttpPost]
        public ActionResult UserAcctionLog(int? pagenum, int? numperpage, string keyword, string keywords)
        {
            var db = new DBEntities();

            if (pagenum == null) pagenum = 1;
            if (numperpage == null) numperpage = 20;
            ViewBag.pageNum = pagenum.Value;
            ViewBag.numPerPage = numperpage.Value;
            int skipnum = (pagenum.Value - 1)*numperpage.Value;

            keyword = keyword ?? "";
            ViewBag.keywords = keyword = (keyword == "" && keywords != null) ? keywords : keyword;

            IQueryable<CommonLog> model = db.CommonLog.Where(m => keyword == "" 
                || m.theuser.Contains(keyword));
            ViewBag.totalnum = model.Count();
            model = model.OrderBy(m => m.id).Skip(skipnum).Take(numperpage.Value);

            return View(model);
        }

        [HttpGet]
        public ActionResult SystemErrorLog()
        {
            var db = new DBEntities();
            ViewBag.pageNum = 1;
            ViewBag.numPerPage = 20;
            ViewBag.totalnum = db.Log.Count();
            return View(db.Log.Take(20).ToList());
        }

        [HttpPost]
        public ActionResult SystemErrorLog(int? pagenum, int? numperpage)
        {
            var db = new DBEntities();

            if (pagenum == null) pagenum = 1;
            if (numperpage == null) numperpage = 20;
            ViewBag.pageNum = pagenum.Value;
            ViewBag.numPerPage = numperpage.Value;
            int skipnum = (pagenum.Value - 1)*numperpage.Value;

            IQueryable<Log> model = db.Log;
            ViewBag.totalnum = model.Count();
            model = model.OrderBy(m => m.LogID).Skip(skipnum).Take(numperpage.Value);

            return View(model);
        }
    }
}